package com.supreme.threadserver.component;

import com.supreme.threadserver.entity.AbsThreadPool;
import com.supreme.threadserver.entity.ThreadPoolInfo;
import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

@Component
public class ThreadPoolData {

    private static Constructor<?> getClazzStep1(Class<?> clazz) throws NoSuchMethodException {
        return clazz.getConstructor();
    }

    private static Constructor<?> getClazzStep2(Class<?> clazz) throws NoSuchMethodException {
        return ReflectionUtils.accessibleConstructor(clazz);
    }

    private static Constructor<?> getClazzStep3(Class<?> clazz) throws NoSuchMethodException {
        return ConstructorUtils.getAccessibleConstructor(clazz);
    }

    @Bean
    public ThreadPoolInfo threadPoolInfo() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        final Class<?> clazz = Class.forName("com.supreme.threadserver.entity.ThreadPoolInfo");

        final Constructor<?> constructor = getClazzStep2(clazz);
        final AbsThreadPool newInstance = (AbsThreadPool) constructor.newInstance();
        newInstance.printMessage();

        return (ThreadPoolInfo) newInstance;
    }
}
